package com.herui.ota.dao.desk;

import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Files;

@Component
public class FileDeskStore {

    public InputStream getDownloadStream(String path) throws FileNotFoundException {
        File file = new File(path);
        if (!file.exists()){
            return null;
        }

        return new FileInputStream(file);
    }

    public OutputStream getUploadStream(String path ) throws IOException {

        File file = new File(path);
        File parentFile = file.getParentFile();
        if (parentFile != null && !parentFile.exists()){
            Files.createDirectories(parentFile.toPath());
        }

        return new FileOutputStream(file);
    }

    public static void main(String[] args) throws IOException {
        String path = "desk\\test\\1.txt";

        FileDeskStore fileDeskStore = new FileDeskStore();
        OutputStream uploadStream = fileDeskStore.getUploadStream(path);
        uploadStream.write(1);
        uploadStream.close();
    }
}
